﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class BaseData_ServiceStationDetail : System.Web.UI.Page
{
    private string Code
    {
        get
        {
            return Request.QueryString["code"] != null ? Request.QueryString["code"].ToString() : "";
        }
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    private Model.ServiceSites ServiceSite
    {
        get
        {
            Model.ServiceSites serviceSite = null;
            if (ViewState["ServiceSite"] != null)
            {
                serviceSite = (Model.ServiceSites)ViewState["ServiceSite"];
            }
            return serviceSite;
        }
        set
        {
            ViewState["ServiceSite"] = value;
        }
    }

    protected void BindDropProvince()
    {
        List<Model.Province> provinces = new BLL.Province().GetAllList();
        Province.DataSource = provinces;
        Province.DataTextField = "ProName";
        Province.DataValueField = "ProId";
        Province.DataBind();
        ListItem item = new ListItem();
        item.Value = "-1";
        item.Text = "";
        Province.Items.Insert(0, item);
    }

    private void BindDropAdmin()
    {
        List<Model.USERS> users = new BLL.USERS().GetAllList();
        dropAdmin.DataSource = users;
        dropAdmin.DataTextField = "USERS_NAME";
        dropAdmin.DataValueField = "USERS_CODE";
        dropAdmin.DataBind();
        ListItem item = new ListItem();
        item.Value = "-1";
        item.Text = "";
        dropAdmin.Items.Insert(0, item);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropProvince();
            BindDropAdmin();
            Bind();
        }
    }

    private void Bind()
    {
        Model.ServiceSites station = new BLL.ServiceSites().GetModel(Convert.ToInt32(this.Code));
        if (station == null)
        {
            Response.Write("<script>alert('系统无法读取到该服务站信息！');window.location.href='ServiceStationsMain.aspx'</script>");
        }
        this.ServiceSite = station;
        lblServiceId.Text = station.SS_ID.ToString();
        txtServiceStationName.Text = station.SS_Name;
        txtPhone1.Text = station.Phone1;
        txtPhone2.Text = station.Phone2;
        hidReceiveAddressInfo.Value = station.SS_ProId + "$" + station.SS_CityId + "$" + station.SS_AreaId;
        txtAddress.Text = station.SS_Address;
        for (int i = 0; i < dropAdmin.Items.Count; i++)
        {
            if (dropAdmin.Items[i].Value == station.SS_admin)
            {
                dropAdmin.SelectedIndex = i;
                break;
            }
        }
        if (station.SS_IsDel == 1)
        {
            rdoYes.Checked = true;
            rdoNo.Checked = false;
        }
        else
        {
            rdoYes.Checked = false;
            rdoNo.Checked = true;
        }
    }

    private void ValidShipAddressInfo()
    {
        if (txtServiceStationName.Text == "")
        {
            throw new Exception("请填写服务站名称！");
        }
        if (txtPhone1.Text == "" && txtPhone2.Text == "")
        {
            throw new Exception("联系电话必须至少填写一个！");
        }
        if (txtAddress.Text == "")
        {
            throw new Exception("请填写服务站详细地址！");
        }
        if (dropAdmin.SelectedIndex == 0)
        {
            throw new Exception("请选择服务站管理员！");
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (!Check.CheckPermission("100100109", "00001"))
            {
                Share.Alert(Page, "无权操作"); return;
            }
            ValidShipAddressInfo();
            
            if (new BLL.ServiceSites().UpdateServiceStation(this.ServiceSite, txtServiceStationName.Text, txtPhone1.Text, txtPhone2.Text, Province.Items[Province.SelectedIndex].Value, hidCityCode.Value, hidAreaCode.Value, txtAddress.Text, dropAdmin.SelectedValue,rdoYes.Checked))
            {
                Share.InsertLog("100100109", "00002");
                Response.Write("<script>alert('修改服务站成功！');window.location.href='ServiceStationDetail.aspx?code=" + this.Code + "'</script>");
            }
            else
            {
                Share.Alert(this.Page, "修改服务站失败！");
            }

        }
        catch (Exception ex)
        {
            Share.Alert(this.Page, ex.Message);
        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

    }

}
